package asia.lxq.cic.controller;

import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.entity.Comment;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.User;
import asia.lxq.cic.service.CommentService;
import asia.lxq.cic.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    CommentService commentService;
    @PostMapping("/post_comment")
    public Result<Object> postComment(@RequestBody Comment comment){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
        comment.setCommentUserId(user.getUserId());
        comment.setCommentThumb(0);
        commentService.postComment(comment);
        return Result.success("发布成功");
    }
}
